﻿using System;
using System.Linq;
using System.Text;
using Kaidou.Model;
using System.Data;
using System.Data.SqlClient;
using Kaidou.Util;
using System.Collections;


namespace Kaidou.Persister
{
    public class TRN_Report_D_Persister
    {

        /**
	     * TRN_Report_DテーブルからTRN_Report_Dのリストを取得します。
	     * @param	con				DBコネクション
	     * @param	report_no 		報告No.
	     * @return	List			報告明細モデルのリスト
	    */

	    public ArrayList get( string report_no ){
		    ArrayList list = new ArrayList();
		    
            TrnReportd model = null;
            string sql =
			    "select "
				    + " report_no,"
				    + " report_rec_no,"
				    + " seal_str_no,"
				    + " seal_end_no,"
				    + " machine_clss,"
				    + " reg_date,"
				    + " upd_date,"
				    + " upd_union_code,"
				    + " upd_branch_code,"
				    + " upd_func_id"
				    + " from TRN_Report_D "
                    + " where report_no = '" + report_no + "'"
				    + " order by report_no";
            SqlDB db = new SqlDB();
            try
            {


                SqlParameter[] sqlParameter = { };
                SqlDataReader dataReader = db.GetDataCmd(sql, sqlParameter);

                while (dataReader.Read())
                {
                    model = new TrnReportd();
                    TrnReportdId id = new TrnReportdId();
                    id.ReportNo = (string)dataReader["report_no"];
                    id.ReportRecNo = (short)dataReader["report_rec_no"];
                    model.Id = id;
                    model.SealStrNo = (string)dataReader["seal_str_no"];
                    model.SealEndNo = (string)dataReader["seal_end_no"];
                    model.MachineClss = (string)dataReader["machine_clss"];
                    model.RegDate = (DateTime)dataReader["reg_date"];
                    model.UpdDate = (DateTime)dataReader["upd_date"];
                    model.UpdUnionCode = (string)dataReader["upd_union_code"];
                    model.UpdBranchCode = (short)dataReader["upd_branch_code"];
                    model.UpdFuncId = (string)dataReader["upd_func_id"];

                    list.Add(model);
                }
            }
            catch (Exception ee)
            {
                LogManager.error("TRN_Report_D_Persister get error " + ee.Message);
            }
            finally
            {
                db.Close();
            }

            return list;
	    }


        /**
	     * TRN_Report_Dテーブルにレコードを挿入します。
	     * @param	con	DBコネクション
	     * @param	model 報告詳細モデル
	     * @return	boolean
	     * @throws	PersisterSQLException
	    */
        public bool insert(SqlDB db, TrnReportd M)
        {
		   string sql =
			    "insert into TRN_Report_D "
				    + "(report_no,"
				    + " report_rec_no,"
				    + " seal_str_no,"
				    + " seal_end_no,"
				    + " machine_clss,"
				    + " reg_date,"
				    + " upd_date,"
				    + " upd_union_code,"
				    + " upd_branch_code,"
				    + " upd_func_id)"
				    + "values( "
				    + " @report_no ,"
				    + " @report_rec_no ,"
				    + " @seal_str_no ,"
				    + " @seal_end_no ,"
				    + " @machine_clss ,"
				    + " @reg_date ,"
				    + " @upd_date ,"
				    + " @upd_union_code ,"
				    + " @upd_branch_code ,"
				    + " @upd_func_id )";

           LogManager.info("TRN_Report_D_Persister Insert sql : " + sql);
           try
           {

               SqlParameter[] sqlParameter = {   
                                                new SqlParameter("@report_no",SqlDbType.VarChar,10),
                                                new SqlParameter("@report_rec_no",SqlDbType.SmallInt),
                                                new SqlParameter("@seal_str_no",SqlDbType.VarChar,9),
                                                new SqlParameter("@seal_end_no",SqlDbType.VarChar,9),
                                                new SqlParameter("@machine_clss",SqlDbType.VarChar,1),
                                                new SqlParameter("@reg_date",SqlDbType.DateTime),
                                                new SqlParameter("@upd_date",SqlDbType.DateTime),
                                                new SqlParameter("@upd_union_code",SqlDbType.VarChar,5),
                                                new SqlParameter("@upd_branch_code",SqlDbType.SmallInt),
                                                new SqlParameter("@upd_func_id",SqlDbType.VarChar,20)
                                              };

               sqlParameter[0].Value = M.Id.ReportNo;
               sqlParameter[1].Value = M.Id.ReportRecNo;
               sqlParameter[2].Value = M.SealStrNo;
               sqlParameter[3].Value = M.SealEndNo;
               sqlParameter[4].Value = M.MachineClss;
               sqlParameter[5].Value = M.RegDate;
               sqlParameter[6].Value = M.UpdDate;
               sqlParameter[7].Value = M.UpdUnionCode;
               sqlParameter[8].Value = M.UpdBranchCode;
               sqlParameter[9].Value = M.UpdFuncId == null ? "" : M.UpdFuncId;


               db.ExecuteCmd(sql, sqlParameter);
               
           }
           catch (Exception e)
           {

               LogManager.error("TRN_Report_D_Persister Insert " + e.Message + e.Source +e.StackTrace);
               throw new Exception("TRN_Report_D_Persister insert  error");

           } 

		    return true;
	    }
   
        /**
	     * TRN_Report_Dテーブルからレコード論理削除します。
	     * @param	con				DBコネクション
	     * @param	report_no	 	報告No.
	     * @param	report_rec_no	行No.
	     * @return	boolean
	    */
	    public bool delete(SqlDB db , string report_no, int report_rec_no)  {
		    
		    string sql = "delete from trn_report_d where report_no = '"+report_no+"' and report_rec_no = "+report_rec_no;

		    try {
			   
               // SqlDB db=new SqlDB();
               db.ExecuteCmd(sql,new SqlParameter[]{});

		    } catch (Exception e) {

                LogManager.error(" TRN_Report_D_Persister "+e.Message+e.StackTrace);
                return false;
		    }
		    return true;
	    }
    
        /**
	     * TRN_Report_Dテーブルからレコードを取得します。
	     * @param	con				DBコネクション
	     * @param	report_no 		報告No.
	     * @param	report_rec_no	行No.
	     * @return	TRN_Report_D	報告明細モデル
	    */
	    public TrnReportd get(string report_no, int report_rec_no) {
		    
		    TrnReportd model = null;
            SqlDB db = new SqlDB();
		    string sql =
			    "select "
				    + " report_no,"
				    + " report_rec_no,"
				    + " seal_str_no,"
				    + " seal_end_no,"
				    + " machine_clss,"
				    + " reg_date,"
				    + " upd_date,"
				    + " upd_union_code,"
				    + " upd_branch_code,"
				    + " upd_func_id"
				    + " from TRN_Report_D "
				    + " where report_no = '"+report_no+"'"
				    + " and report_rec_no = "+report_rec_no+""
				    + " order by report_no";
            try
            {

                SqlDataReader dataReader = db.GetDataCmd(sql, new SqlParameter[] { });
                while (dataReader.Read())
                {
                    model = new TrnReportd();
                    TrnReportdId id = new TrnReportdId();
                    id.ReportNo = (string)dataReader["report_no"];
                    id.ReportRecNo = (short)dataReader["report_rec_no"];
                    model.Id = id;
                    model.SealStrNo = (string)dataReader["seal_str_no"];
                    model.SealEndNo = (string)dataReader["seal_end_no"];
                    model.MachineClss = (string)dataReader["machine_clss"];
                    model.RegDate = (DateTime)dataReader["reg_date"];
                    model.UpdDate = (DateTime)dataReader["upd_date"];
                    model.UpdUnionCode = (string)dataReader["upd_union_code"];
                    model.UpdBranchCode = (short)dataReader["upd_branch_code"];
                    model.UpdFuncId = (string)dataReader["upd_func_id"];

                }
            }
            catch (Exception e)
            {
                LogManager.error("trn_report_d_persister get " + e.Message + e.StackTrace);
            }
            finally
            {
                db.Close();
            }
		    return model;
	    }
        
        /**
	     * TRN_Report_Dテーブルからレコード更新します。
	     * @param	con				DBコネクション
	     * @param	model	 		報告明細モデル
	     * @return	boolean
	    */
	    public bool update( SqlDB db ,TrnReportd M) {
		    LogManager.info(" TRN_Report_D_Persister update - in ");
            
		    string sql =
			    "update TRN_Report_D set " + " seal_str_no = @seal_str_no," + " seal_end_no = @seal_end_no," + " machine_clss = @machine_clss," + " upd_date = @upd_date," + " upd_union_code = @upd_union_code," + " upd_branch_code = @upd_branch_code," + " upd_func_id = @upd_func_id" + " where " + " report_no = @report_no " + " and report_rec_no =@report_rec_no";

		    try {
                SqlParameter[] sqlParameter = {   
                                                new SqlParameter("@report_no",SqlDbType.VarChar,10),
                                                new SqlParameter("@report_rec_no",SqlDbType.SmallInt),
                                                new SqlParameter("@seal_str_no",SqlDbType.VarChar,9),
                                                new SqlParameter("@seal_end_no",SqlDbType.VarChar,9),
                                                new SqlParameter("@machine_clss",SqlDbType.VarChar,1),
                                                new SqlParameter("@upd_date",SqlDbType.DateTime),
                                                new SqlParameter("@upd_union_code",SqlDbType.VarChar,5),
                                                new SqlParameter("@upd_branch_code",SqlDbType.SmallInt),
                                                new SqlParameter("@upd_func_id",SqlDbType.VarChar,20)
                                              };

                sqlParameter[0].Value = M.Id.ReportNo;
                sqlParameter[1].Value = M.Id.ReportRecNo;
                sqlParameter[2].Value = M.SealStrNo;
                sqlParameter[3].Value = M.SealEndNo;
                sqlParameter[4].Value = M.MachineClss;
                sqlParameter[5].Value = M.UpdDate;
                sqlParameter[6].Value = M.UpdUnionCode;
                sqlParameter[7].Value = M.UpdBranchCode;
                sqlParameter[8].Value = M.UpdFuncId == null ? "" : M.UpdFuncId;

                //SqlDB db = new SqlDB();
                db.ExecuteCmd(sql, sqlParameter);

		    } catch (Exception e) {
                LogManager.error(" trn_report_d_persister "+e.StackTrace+e.Message);
                return false;
		    } 
		    return true;
	    }

    
    }
}
